package com.cxs.musicplaywidget.widget.widget1;

import com.cxs.musicplaywidget.service.DeviceManager;
import com.cxs.musicplaywidget.util.LogUtils;
import com.cxs.musicplaywidget.widget.controller.FormController;
import com.cxs.musicplaywidget.widget.controller.WidgetManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;


public class Widget1Impl extends FormController {
    private static final String tag = Widget1Impl.class.getSimpleName();
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, Widget1Impl.class.getName());
    private static final int DIMENSION_1X2 = 1;
    private static final int DIMENSION_2X2 = 2;
    private static final int DIMENSION_2X4 = 3;
    private static final int DIMENSION_4X4 = 4;
    WidgetManager widgetManager;

    public Widget1Impl(Context context, String formName, Integer dimension) {
        super(context, formName, dimension);
        widgetManager = WidgetManager.getInstance(context);
        widgetManager.init();
    }

    @Override
    public ProviderFormInfo bindFormData(long formId) {
        HiLog.info(TAG, "bind form data");
        DeviceManager deviceManager = DeviceManager.getInstance(context);
        LogUtils.debug(tag, "bindFormData.deviceManager.getDeviceList()" + deviceManager.getDeviceList());
        ZSONObject zsonObject = new ZSONObject();
        ProviderFormInfo providerFormInfo = new ProviderFormInfo();
        if (dimension == DIMENSION_1X2) {
            zsonObject.put("dim1X2", true);
        }
        if (dimension == DIMENSION_2X2) {
            zsonObject.put("dim2X2", true);
        }
        if (dimension == DIMENSION_2X4) {
            zsonObject.put("dim2X4", true);
        }
        if (dimension == DIMENSION_4X4) {
            zsonObject.put("dim4X4", true);
        }
        providerFormInfo.setJsBindingData(new FormBindingData(zsonObject));
        return providerFormInfo;
    }

    @Override
    public void updateFormData(long formId, Object... vars) {
        HiLog.info(TAG, "update form data timing, default 30 minutes");
    }

    @Override
    public void onTriggerFormEvent(long formId, String message) {
        HiLog.info(TAG, "handle card click event.");
        ZSONObject zsonObject = ZSONObject.stringToZSON(message);
        String action = zsonObject.getString("action");
        ZSONObject param = zsonObject.getZSONObject("param");
        WidgetManager widgetManager = WidgetManager.getInstance(context);
        widgetManager.sendMessageEvent(action, param);
    }

    @Override
    public Class<? extends AbilitySlice> getRoutePageSlice(Intent intent) {
        HiLog.info(TAG, "get the default page to route when you click card.");
        return null;
    }


}
